

<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>OneDrive &mdash; O365  documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=e59714d7" />

  
      <script src="../_static/jquery.js?v=5d32c60e"></script>
      <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
      <script src="../_static/documentation_options.js?v=5929fcd5"></script>
      <script src="../_static/doctools.js?v=9bcbadda"></script>
      <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Planner" href="planner.html" />
    <link rel="prev" title="Mailbox" href="mailbox.html" />
    <link href="../_static/css/style.css" rel="stylesheet" type="text/css">

</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html">
            
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../getting_started.html">Getting Started</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../usage.html">Detailed Usage</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="connection.html">Protocols</a></li>
<li class="toctree-l2"><a class="reference internal" href="connection.html#resources">Resources</a></li>
<li class="toctree-l2"><a class="reference internal" href="account.html">Account</a></li>
<li class="toctree-l2"><a class="reference internal" href="account.html#account-class-and-modularity">Account Class and Modularity</a></li>
<li class="toctree-l2"><a class="reference internal" href="addressbook.html">Address Book</a></li>
<li class="toctree-l2"><a class="reference internal" href="calendar.html">Calendar</a></li>
<li class="toctree-l2"><a class="reference internal" href="directory.html">Directory and Users</a></li>
<li class="toctree-l2"><a class="reference internal" href="excel.html">Excel</a></li>
<li class="toctree-l2"><a class="reference internal" href="group.html">Group</a></li>
<li class="toctree-l2"><a class="reference internal" href="mailbox.html">Mailbox</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">OneDrive</a></li>
<li class="toctree-l2"><a class="reference internal" href="planner.html">Planner</a></li>
<li class="toctree-l2"><a class="reference internal" href="sharepoint.html">Sharepoint</a></li>
<li class="toctree-l2"><a class="reference internal" href="tasks.html">Tasks</a></li>
<li class="toctree-l2"><a class="reference internal" href="teams.html">Teams</a></li>
<li class="toctree-l2"><a class="reference internal" href="utils.html">Utils</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../api.html">O365 API</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">O365</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="../usage.html">Detailed Usage</a></li>
      <li class="breadcrumb-item active">OneDrive</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../_sources/usage/onedrive.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
        <a href="mailbox.html" class="btn btn-neutral float-left" title="Mailbox" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="planner.html" class="btn btn-neutral float-right" title="Planner" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
  </div>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="onedrive">
<h1>OneDrive<a class="headerlink" href="#onedrive" title="Link to this heading"></a></h1>
<p>The <code class="docutils literal notranslate"><span class="pre">Storage</span></code> class handles all functionality around One Drive and Document Library Storage in SharePoint.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">Storage</span></code> instance allows retrieval of <code class="docutils literal notranslate"><span class="pre">Drive</span></code> instances which handles all the Files
and Folders from within the selected <code class="docutils literal notranslate"><span class="pre">Storage</span></code>. Usually you will only need to work with the
default drive. But the <code class="docutils literal notranslate"><span class="pre">Storage</span></code> instances can handle multiple drives.</p>
<p>A <code class="docutils literal notranslate"><span class="pre">Drive</span></code> will allow you to work with Folders and Files.</p>
<p>These are the scopes needed to work with the <code class="docutils literal notranslate"><span class="pre">Storage</span></code>, <code class="docutils literal notranslate"><span class="pre">Drive</span></code> and <code class="docutils literal notranslate"><span class="pre">DriveItem</span></code> classes.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Raw Scope</p></th>
<th class="head"><p>Included in Scope Helper</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Files.Read</p></td>
<td><p>—</p></td>
<td><p>To only read my files</p></td>
</tr>
<tr class="row-odd"><td><p>Files.Read.All</p></td>
<td><p>onedrive</p></td>
<td><p>To only read all the files the user has access</p></td>
</tr>
<tr class="row-even"><td><p>Files.ReadWrite</p></td>
<td><p>—</p></td>
<td><p>To read and save my files</p></td>
</tr>
<tr class="row-odd"><td><p>Files.ReadWrite.All</p></td>
<td><p>onedrive_all</p></td>
<td><p>To read and save all the files the user has access</p></td>
</tr>
</tbody>
</table>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">account</span> <span class="o">=</span> <span class="n">Account</span><span class="p">(</span><span class="n">credentials</span><span class="o">=</span><span class="n">my_credentials</span><span class="p">)</span>

<span class="n">storage</span> <span class="o">=</span> <span class="n">account</span><span class="o">.</span><span class="n">storage</span><span class="p">()</span>  <span class="c1"># here we get the storage instance that handles all the storage options.</span>

<span class="c1"># list all the drives:</span>
<span class="n">drives</span> <span class="o">=</span> <span class="n">storage</span><span class="o">.</span><span class="n">get_drives</span><span class="p">()</span>

<span class="c1"># get the default drive</span>
<span class="n">my_drive</span> <span class="o">=</span> <span class="n">storage</span><span class="o">.</span><span class="n">get_default_drive</span><span class="p">()</span>  <span class="c1"># or get_drive(&#39;drive-id&#39;)</span>

<span class="c1"># get some folders:</span>
<span class="n">root_folder</span> <span class="o">=</span> <span class="n">my_drive</span><span class="o">.</span><span class="n">get_root_folder</span><span class="p">()</span>
<span class="n">attachments_folder</span> <span class="o">=</span> <span class="n">my_drive</span><span class="o">.</span><span class="n">get_special_folder</span><span class="p">(</span><span class="s1">&#39;attachments&#39;</span><span class="p">)</span>

<span class="c1"># iterate over the first 25 items on the root folder</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">root_folder</span><span class="o">.</span><span class="n">get_items</span><span class="p">(</span><span class="n">limit</span><span class="o">=</span><span class="mi">25</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">item</span><span class="o">.</span><span class="n">is_folder</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">get_items</span><span class="p">(</span><span class="mi">2</span><span class="p">)))</span>  <span class="c1"># print the first to element on this folder.</span>
    <span class="k">elif</span> <span class="n">item</span><span class="o">.</span><span class="n">is_file</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">item</span><span class="o">.</span><span class="n">is_photo</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">camera_model</span><span class="p">)</span>  <span class="c1"># print some metadata of this photo</span>
        <span class="k">elif</span> <span class="n">item</span><span class="o">.</span><span class="n">is_image</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">dimensions</span><span class="p">)</span>  <span class="c1"># print the image dimensions</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># regular file:</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">mime_type</span><span class="p">)</span>  <span class="c1"># print the mime type</span>
</pre></div>
</div>
<p>Both Files and Folders are DriveItems. Both Image and Photo are Files, but Photo is also an Image. All have some different methods and properties. Take care when using ‘is_xxxx’.</p>
<p>When copying a DriveItem the api can return a direct copy of the item or a pointer to a resource that will inform on the progress of the copy operation.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># copy a file to the documents special folder</span>

<span class="n">documents_folder</span> <span class="o">=</span> <span class="n">my_drive</span><span class="o">.</span><span class="n">get_special_folder</span><span class="p">(</span><span class="s1">&#39;documents&#39;</span><span class="p">)</span>

<span class="n">files</span> <span class="o">=</span> <span class="n">my_drive</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s1">&#39;george best quotes&#39;</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="k">if</span> <span class="n">files</span><span class="p">:</span>
    <span class="n">george_best_quotes</span> <span class="o">=</span> <span class="n">files</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">operation</span> <span class="o">=</span> <span class="n">george_best_quotes</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">documents_folder</span><span class="p">)</span>  <span class="c1"># operation here is an instance of CopyOperation</span>

    <span class="c1"># to check for the result just loop over check_status.</span>
    <span class="c1"># check_status is a generator that will yield a new status and progress until the file is finally copied</span>
    <span class="k">for</span> <span class="n">status</span><span class="p">,</span> <span class="n">progress</span> <span class="ow">in</span> <span class="n">operation</span><span class="o">.</span><span class="n">check_status</span><span class="p">():</span>  <span class="c1"># if it&#39;s an async operations, this will request to the api for the status in every loop</span>
        <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">status</span><span class="si">}</span><span class="s2"> - </span><span class="si">{</span><span class="n">progress</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>  <span class="c1"># prints &#39;in progress - 77.3&#39; until finally completed: &#39;completed - 100.0&#39;</span>
    <span class="n">copied_item</span> <span class="o">=</span> <span class="n">operation</span><span class="o">.</span><span class="n">get_item</span><span class="p">()</span>  <span class="c1"># the copy operation is completed so you can get the item.</span>
    <span class="k">if</span> <span class="n">copied_item</span><span class="p">:</span>
        <span class="n">copied_item</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>  <span class="c1"># ... oops!</span>
</pre></div>
</div>
<p>You can also work with share permissions:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">current_permisions</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">get_permissions</span><span class="p">()</span>  <span class="c1"># get all the current permissions on this drive_item (some may be inherited)</span>

<span class="c1"># share with link</span>
<span class="n">permission</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">share_with_link</span><span class="p">(</span><span class="n">share_type</span><span class="o">=</span><span class="s1">&#39;edit&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">permission</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">permission</span><span class="o">.</span><span class="n">share_link</span><span class="p">)</span>  <span class="c1"># the link you can use to share this drive item</span>
<span class="c1"># share with invite</span>
<span class="n">permission</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">share_with_invite</span><span class="p">(</span><span class="n">recipients</span><span class="o">=</span><span class="s1">&#39;george_best@best.com&#39;</span><span class="p">,</span> <span class="n">send_email</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="s1">&#39;Greetings!!&#39;</span><span class="p">,</span> <span class="n">share_type</span><span class="o">=</span><span class="s1">&#39;edit&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">permission</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">permission</span><span class="o">.</span><span class="n">granted_to</span><span class="p">)</span>  <span class="c1"># the person you share this item with</span>
</pre></div>
</div>
<p>You can also:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># download files:</span>
<span class="n">file</span><span class="o">.</span><span class="n">download</span><span class="p">(</span><span class="n">to_path</span><span class="o">=</span><span class="s1">&#39;/quotes/&#39;</span><span class="p">)</span>

<span class="c1"># upload files:</span>

<span class="c1"># if the uploaded file is bigger than 4MB the file will be uploaded in chunks of 5 MB until completed.</span>
<span class="c1"># this can take several requests and can be time consuming.</span>
<span class="n">uploaded_file</span> <span class="o">=</span> <span class="n">folder</span><span class="o">.</span><span class="n">upload_file</span><span class="p">(</span><span class="n">item</span><span class="o">=</span><span class="s1">&#39;path_to_my_local_file&#39;</span><span class="p">)</span>

<span class="c1"># restore versions:</span>
<span class="n">versions</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">get_versions</span><span class="p">()</span>
<span class="k">for</span> <span class="n">version</span> <span class="ow">in</span> <span class="n">versions</span><span class="p">:</span>
    <span class="k">if</span> <span class="n">version</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;2.0&#39;</span><span class="p">:</span>
        <span class="n">version</span><span class="o">.</span><span class="n">restore</span><span class="p">()</span>  <span class="c1"># restore the version 2.0 of this file</span>

<span class="c1"># ... and much more ...</span>
</pre></div>
</div>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="mailbox.html" class="btn btn-neutral float-left" title="Mailbox" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="planner.html" class="btn btn-neutral float-right" title="Planner" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2025, alejcas.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>